package com.voute.View.Activity;

import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.SimpleNotificationBean;
import com.voute.Model.Bean.SimpleNotificationEntity;
import com.voute.Presenter.IPushNotificationPresenter;
import com.voute.Presenter.Impl.PushNotificationPresenterImpl;
import com.voute.R;
import com.voute.Utils.PreferenceUtils;
import com.voute.View.Adapter.PushMessageAdapter;
import com.voute.View.IView.IPushMessageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xianke on 22/04/2017.
 */

public class SimplePushMessageActivity extends BaseActivity implements IPushMessageView {

    private ListView content_list;
    private SwipeRefreshLayout swipe_refresh_layout;
    private TextView tv_unread_num;
    private RelativeLayout layout_set_all_readed;
    private RelativeLayout back_layout;

    private List<SimpleNotificationEntity> datas = new ArrayList<>();
    private PushMessageAdapter adapter;
    private IPushNotificationPresenter presenter;

    private boolean isRefresh = true;
    private int page = 1;
    private boolean stopLoadMore;
    private boolean isLoading;

    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_push_msg;
    }

    @Override
    public void setupView() {
        content_list = $(R.id.content_list);
        swipe_refresh_layout = $(R.id.swipe_refresh_layout);
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                doRequestNotification();
                presenter.requestUnreadNotificationNum();
            }
        });
        tv_unread_num = $(R.id.tv_unread_num);
        layout_set_all_readed = $(R.id.layout_set_all_readed);
        back_layout = $(R.id.back_layout);
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.GetInstance().finishActivity(SimplePushMessageActivity.this);
            }
        });
        layout_set_all_readed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(SimplePushMessageActivity.this).title("提示").content("确定全部设置为已读?").positiveText("确定").negativeText("取消").onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        presenter.setAllNessageReaded();
                    }
                }).show();
            }
        });
        adapter = new PushMessageAdapter(this, datas);
        content_list.setAdapter(adapter);
        content_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int last_visiable = absListView.getLastVisiblePosition();
                if (!stopLoadMore && !isLoading && (last_visiable > 0)&&(!TextUtils.isEmpty(PreferenceUtils.GetUserToken()))) {
                    if (last_visiable + 2 > totalItemCount) {
                        isRefresh = false;
                        doRequestNotification();
                    }
                }
            }
        });
    }

    private void doRequestNotification() {
        int page_local = 1;
        if (isRefresh)
            page_local = 1;
        else {
            if (datas.size() == 0) {
                page_local = 1;
            } else {
                page_local = page;
            }
        }
        if (!isRefresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                isLoading = true;
            }
        } else {
            isLoading = true;
            stopLoadMore = true;
        }
        presenter.requestPushNotifications(page_local + "");
    }

    @Override
    public void setupData() {
        presenter = new PushNotificationPresenterImpl(this, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        isRefresh = true;
        doRequestNotification();
        presenter.requestUnreadNotificationNum();
    }

    @Override
    public void refreshMessages(SimpleNotificationBean response) {
        if (response != null) {
            if (response.getStatus() == 0) {
                isLoading = false;
                if (isRefresh) {
                    page = 1;
                    datas.clear();
                    isRefresh = false;
                }
                if (response.getData() != null) {
                    if (response.getData().getFeeds() != null && response.getData().getFeeds().size() > 0) {
                        page = page + 1;
                        datas.addAll(response.getData().getFeeds());
                        adapter.setDatas(datas);
                        stopLoadMore = false;
                    } else {
                        stopLoadMore = true;
                    }
                } else {
                    isLoading = false;
                    stopLoadMore = false;
                }
            } else {
                isLoading = false;
                stopLoadMore = false;
            }
        } else {
            isLoading = false;
            stopLoadMore = false;
        }
    }

    @Override
    public void setUnreadMessageNum(int unreadMessageNum) {
        tv_unread_num.setText(unreadMessageNum+"");
    }

    @Override
    public void setAllReadedSuccess() {
        presenter.requestUnreadNotificationNum();
        isRefresh=true;
        doRequestNotification();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        swipe_refresh_layout.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
        swipe_refresh_layout.setRefreshing(false);
        MaterialDialog notice_dialog = new MaterialDialog.Builder(SimplePushMessageActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }
}
